package com.wallet.common.jwt;

import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class JWTUtil {
	private static String SECRET = "jwt2016";
	
	private static String ISS = "qj.pj";
	
	public static int MAX_AGE = 604800;

	public static int MAX_AGE_PC = 3600;

	public static String TOKEN_CLAIMS_KEY = "tokenClaims";
	 
	public static String VALIDETE_RESULT_KEY = "isValid";
	
	public static String CLAIMS_KEY_QUID = "quid";
	
	public static String CLAIMS_KEY_PUID = "puid";
	
	public static String HEADER_KEY = "qpToken";
	
	public static String AJAX_RETURN_KEY = "qpToken";
	
	public static String COOKIE_KEY = "qpToken";
	
	public static String COOKIE_PATH = "/";
	
	public static String AUD_QJ = "www.qianjialicai.com";
	
	public static String AUD_PJ = "www.piaojiazi.com";
	
    public static String getQPToken(String pid , String qid){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put(JWTUtil.CLAIMS_KEY_PUID, StringUtils.isEmpty(pid) ? "" : pid);//票夹用户ID
		params.put(JWTUtil.CLAIMS_KEY_QUID, StringUtils.isEmpty(qid) ? "" : qid);//钱夹用户ID
		return JWTUtil.sign(params);
    }
	
	private static HashMap<String, Object> getBaseClaims(long iat, long exp) {
		HashMap<String, Object> claims = new HashMap<String, Object>();
		claims.put("iss", ISS);
		claims.put("exp", exp);
		claims.put("iat", iat);
		List<String> audList = new ArrayList<String>();
		audList.add(AUD_QJ);
		audList.add(AUD_PJ);
		claims.put("aud", audList);
		return claims;
	}

	public static String sign(HashMap<String,Object> params) {
		/*Map<String, Object> params = new HashMap<String, Object>();
		params.put(CLAIMS_KEY_QUID, member.getMemberId());
		params.put(CLAIMS_KEY_PUID, member.getPuid());*/
		return sign(params);
	}



	public static String sign(Map<String, Object> params) {
		long iat = System.currentTimeMillis() / 1000l; // issued at claim
		long exp = iat + MAX_AGE;
		JWTSigner signer = new JWTSigner(SECRET);
		HashMap<String, Object> claims = getBaseClaims(iat, exp);
		if(params != null && params.size() > 0) {
			claims.putAll(params);
		}
		return signer.sign(claims);
	}

	public static String refresh(Map<String, Object> claims) {
		long iat = System.currentTimeMillis() / 1000l; // issued at claim 
		long exp = iat + MAX_AGE;
		JWTSigner signer = new JWTSigner(SECRET);
		claims.put("exp", exp);
		claims.put("iat", iat);
		return signer.sign(claims);
	}

	public static Map<String, Object> verify(String token) {
		Map<String,Object> rtn = new HashMap<String, Object>();
		try {
		    final JWTVerifier verifier = new JWTVerifier(SECRET, AUD_PJ, ISS);
		    final Map<String,Object> claims= verifier.verify(token);
		    rtn.putAll(claims);
		    rtn.put(VALIDETE_RESULT_KEY, true);
		} catch (Exception e) {
			//e.printStackTrace();
			rtn.put(VALIDETE_RESULT_KEY, false);
			System.err.println("过期token："+token);
		}
		return rtn;
	}

}
